package cfca.paperless.client.servlet.performance;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.io.FileUtils;

import cfca.paperless.client.bean.ChannelBean;
import cfca.paperless.client.bean.MultiDataBean;
import cfca.paperless.client.bean.PdfBean;
import cfca.paperless.client.bean.SealUserInfo;
import cfca.paperless.client.bean.SignLocation;
import cfca.paperless.client.servlet.PaperlessClient;
import cfca.paperless.client.servlet.PaperlessConfig;
import cfca.paperless.client.util.Base64;
import cfca.paperless.client.util.GUID;
import cfca.paperless.client.util.IoUtil;
import cfca.paperless.client.util.PaperlessUtil;
import cfca.paperless.client.util.StrategyUtil;

public class testCompoundSealAutoPdfPerformance {
    static String proofHashAndMultiData1[];
    static byte[] handwritingImage1;

    public static void main(String[] args) throws Exception {

        try {
            //PaperlessClient clientBean = new PaperlessClient(PaperlessConfig.url);
            
///////////////////////////如下为用户1的数据，如果没用用户1此处可以省略/////////////////////////////////////////////////
            handwritingImage1 = FileUtils.readFileToByteArray(new File("./TestData/handwriting.png"));
            byte[] handwritingData1 = handwritingImage1;
            byte[] identificationFrontData = FileUtils.readFileToByteArray(new File("./TestData/1.png"));
            byte[] identificationBackData = FileUtils.readFileToByteArray(new File("./TestData/2.png"));
            List<MultiDataBean> list1 = new ArrayList<MultiDataBean>();
            list1.add(new MultiDataBean(handwritingData1, "handwritingData.dat", "descr", "2"));
            list1.add(new MultiDataBean(handwritingImage1, "handwritingImage.png", "2"));
            //list1.add(new MultiDataBean(identificationFrontData, "identificationFrontImage.png", "2"));
            //list1.add(new MultiDataBean(identificationBackData, "identificationBackImage.png", "2"));
            proofHashAndMultiData1 = PaperlessUtil.getProofHashAndMultiData(list1);

///////////////////////////以上为用户1的数据/////////////////////////////////////////////////
///////////////////////////如下为用户2的数据，如果没用用户2此处也可以省略/////////////////////////////////////////////////
            /**
            byte[] handwritingImage2 = FileUtils.readFileToByteArray(new File("./TestData/handwriting2.png"));
            byte[] handwritingData2 = handwritingImage2;
            byte[] recordingData = FileUtils.readFileToByteArray(new File("./TestData/recording.mp3"));
            List<MultiDataBean> list2 = new ArrayList<MultiDataBean>();
            list2.add(new MultiDataBean(handwritingData2, "handwritingData.dat", "2"));
            list2.add(new MultiDataBean(handwritingImage2, "handwritingImage.png", "2"));
            list2.add(new MultiDataBean(recordingData, "recording.mav", "0"));
            String proofHashAndMultiData2[] = PaperlessUtil.getProofHashAndMultiData(list2);

            signLocation = new SignLocation(1, 200, 100);
            sealUserInfo = new SealUserInfo("张三", "Z", "aaa44588ddddd666ggg", "15488965698", "北京", "签合同");
            String proofSealStrategyXml2 = StrategyUtil.createProofSealStrategyXml(handwritingImage2, sealUserInfo, signLocation, proofHashAndMultiData2[0]);
            **/
            // ///////////////////////////////////////////////////////
            ///////////////////////////以上为用户2的数据/////////////////////////////////////////////////

            ///////////////////////////如下为加盖机构印章的策略/////////////////////////////////////////////////
            SignLocation signLocation = new SignLocation(1, 350, 300);
            SealUserInfo sealUserInfo = new SealUserInfo("张三五", "北京", "签合同");
            String sealCode = "org001";
            String sealPassword = "11111111";

            String businessCode = "";
            String businessCodeStyle = "font-size:19;x-ratio:0.1;y-ratio:0.5";
            String sealStrategyXml = StrategyUtil.createSealStrategyXml(sealCode, sealPassword, sealUserInfo, signLocation);

            byte[] pdfFile = FileUtils.readFileToByteArray(new File("./TestData/autoCalculatePdfHash.pdf"));
            String savedPdfId = GUID.generateId();
            String outputFilePath = "auto";//"D:\\temp\\proof\\" + savedPdfId + ".pdf";
            String multiData = "[" + proofHashAndMultiData1[1] + "]";System.out.println(multiData.length());
            multiData = "";
            String bizSerialNo = GUID.generateId();
            String bizTypeCode = "B001";
            PdfBean pdfBean = new PdfBean(Base64.toBase64String(pdfFile), savedPdfId, bizSerialNo, bizTypeCode, outputFilePath, "6227000015894784559");
            
            long startTime = System.currentTimeMillis();
            for (int i = 0; i < 20; i++) {
                new CompoundSealPerformance(pdfBean, multiData, sealStrategyXml, startTime).start();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}


class CompoundSealPerformance extends Thread {
    static int successCount;
    static int failueCount;
    static long startTime;

    private synchronized static void successAdd() {
        successCount++;
    }

    private synchronized static void failueAdd() {
        failueCount++;
    }

    PdfBean pdfBean;
    String multiData;
    String sealStrategyXml;

    public CompoundSealPerformance(PdfBean pdfBean, String multiData, String sealStrategyXml, long startTime) {
        this.pdfBean = pdfBean;
        this.multiData = multiData;
        this.sealStrategyXml = sealStrategyXml;
        CompoundSealPerformance.startTime = startTime;
    }

    @Override
    public void run() {
        long timeStart = System.currentTimeMillis();
        boolean f = process(pdfBean, multiData, sealStrategyXml);
        System.out.println("time:" + ":" + (System.currentTimeMillis() - timeStart));
    }

    private boolean process(PdfBean pdfBean, String multiData, String sealStrategyXml) {
        try {
            for (int i = 0; i < 1000; i++) {
                PaperlessClient clientBean = new PaperlessClient(PaperlessConfig.url, 10000, 60000);
                String savedPdfId = GUID.generateId();
                String outputFilePath = "/temp/proof/" + savedPdfId + ".pdf";
                PdfBean pdfBean2 = new PdfBean(pdfBean.getPdf(), savedPdfId, savedPdfId, pdfBean.getBizTypeCode(), "","");
                String pdfBeanXml = PaperlessUtil.buildPdfBeanXml(pdfBean2);
                
                SignLocation signLocation = new SignLocation(1, 300, 300);
                SealUserInfo sealUserInfo = new SealUserInfo("张三三", "Z", GUID.generateId(), "15744659823", "北京", "签合同");
//                sealUserInfo.setKeyAlg("SM2");
                String proofSealStrategyXml1 = StrategyUtil.createProofSealStrategyXml(testCompoundSealAutoPdfPerformance.handwritingImage1, sealUserInfo, signLocation, testCompoundSealAutoPdfPerformance.proofHashAndMultiData1[0]);
                String compoundSealStrategyXml = "<List>" + proofSealStrategyXml1 + "" + "</List>";
                String systemNo = GUID.generateId();
                byte[] sealedPdf = clientBean.compoundSealAutoPdf(systemNo,pdfBeanXml, multiData, compoundSealStrategyXml, PaperlessConfig.operatorCode, new ChannelBean(PaperlessConfig.channelCode).toString());
                String errorRsString = PaperlessUtil.isError(sealedPdf);
                if ("".equals(errorRsString)) {
                    CompoundSealPerformance.successAdd();
                } else {
                    System.out.println("error:" + errorRsString);
                    CompoundSealPerformance.failueAdd();
                }
                if(CompoundSealPerformance.successCount % 10 == 0) {
                    float period = (System.currentTimeMillis() - CompoundSealPerformance.startTime)/1000l;
                    float tpc = CompoundSealPerformance.successCount / period;
                    System.out.println(tpc + "==," + period + ",error:" + CompoundSealPerformance.failueCount);
                }
                Thread.sleep(100);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

}
